package com.example.myapplication.TouchDemo;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.content.ClipData;
import android.content.ClipDescription;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.DragEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.myapplication.R;

public class DragDemoActivity extends AppCompatActivity {

    static final String IMAGEVIEW_TAG = "\"已经拖到目标区域了\"";
    ImageView imageView;
    LinearLayout container;
    RelativeLayout topContainer;
    TextView title;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_drag_demo);
        imageView = findViewById(R.id.drag_img);
        imageView.setTag(IMAGEVIEW_TAG);
        container = findViewById(R.id.drag_container);
        topContainer = findViewById(R.id.drag_topContainer);
        title = findViewById(R.id.drag_title);

        imageView.setOnLongClickListener(new View.OnLongClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.N)
            @Override
            public boolean onLongClick(View v) {
                ClipData.Item item = new ClipData.Item((String)v.getTag());
                ClipData data = new ClipData(IMAGEVIEW_TAG,new String[]{ClipDescription.MIMETYPE_TEXT_PLAIN},item);
                v.startDragAndDrop(data, new View.DragShadowBuilder(v),null,0);
                return true;
            }
        });

        topContainer.setOnDragListener(new View.OnDragListener() {
            @Override
            public boolean onDrag(View v, DragEvent event) {
                int action = event.getAction();
                switch (action){
                    case DragEvent.ACTION_DRAG_STARTED:
                        return true;
                    case DragEvent.ACTION_DRAG_ENTERED:
                        return true;
                    case DragEvent.ACTION_DRAG_LOCATION:
                        return true;
                    case DragEvent.ACTION_DRAG_EXITED:
                        return true;
                    case DragEvent.ACTION_DROP:
                        imageView.setX(event.getX()-imageView.getWidth()/2);
                        imageView.setY(event.getY()-imageView.getHeight()/2);
                        return true;
                        case DragEvent.ACTION_DRAG_ENDED:
                        return true;

                }

                return false;
            }
        });

        container.setOnDragListener(new View.OnDragListener() {
            @Override
            public boolean onDrag(View v, DragEvent event) {
                int action = event.getAction();
                switch (action){
                    case DragEvent.ACTION_DRAG_STARTED:
                        if(event.getClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)){
                            return true;
                        }else{
                            return false;
                        }
                    case DragEvent.ACTION_DRAG_ENTERED:
                        container.setBackgroundColor(Color.YELLOW);
                        return true;
                    case DragEvent.ACTION_DRAG_LOCATION:
                        return true;
                    case DragEvent.ACTION_DRAG_EXITED:
                        container.setBackgroundColor(Color.BLUE);
                        title.setText("");
                        return true;
                    case DragEvent.ACTION_DROP:
                        ClipData.Item item = event.getClipData().getItemAt(0);
                        String dragData = item.getText().toString();
                        title.setText(dragData+"\n"+event.getX()+":"+event.getY());
                        return true;
                        case DragEvent.ACTION_DRAG_ENDED:
                        return true;
                }
                return true;
            }
        });
    }
}